
const {ccclass, property} = cc._decorator;

@ccclass
export default class TouchNode extends cc.Component {


    @property(cc.Node)
    rotateNode: cc.Node = null;
    @property(cc.Node)
    editorNode: cc.Node = null;
    @property(cc.Node)
    deleteNode : cc.Node = null;

    thisWorldPos : cc.Vec2 = null;
    startRotateVec : cc.Vec2 = null;
    startRotateAngle : number = null;
    startScale : number = null;
    startAngle : number = null;
    lastDate : number = null;
    lastPos : cc.Vec2 = null;

    bindDesc : cc.Node = null;

    onLoad () {
        this.registPropertyEvent();
    }



    registPropertyEvent(){
        this.node.on(cc.Node.EventType.TOUCH_START,this.moveTouchStart,this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE,this.moveTouchMove,this);

        this.rotateNode.on(cc.Node.EventType.TOUCH_START,this.rotateAndScaleStart,this);
        this.rotateNode.on(cc.Node.EventType.TOUCH_MOVE,this.rotateAndScaleMove,this);

        this.deleteNode.on(cc.Node.EventType.TOUCH_START,this.deleteTouch,this);
        this.editorNode.on(cc.Node.EventType.TOUCH_START,this.editTouch,this);
    }

    bindNode(node){
        this.bindDesc = node;
        this.node.active = true;
        this.updateNewInfo();
    }
    updateNewInfo(){
        this.node.width = this.bindDesc.width * this.bindDesc.scale + 50;
        this.node.height = this.bindDesc.height * this.bindDesc.scale + 50;
        this.node.position = this.bindDesc.position;
        this.node.angle = this.bindDesc.angle;
    }
    clearNode(){
        this.node.active = false;
    }

    moveTouchStart(event){
        this.lastPos = event.getLocation();
    }
    moveTouchMove(event){
        let thisPos = event.getLocation();
        let subPos = thisPos.sub(this.lastPos);
        this.bindDesc.position = this.bindDesc.position.add(subPos);
        if (this.bindDesc.x > cc.winSize.width/2){
            this.bindDesc.x = cc.winSize.width/2;
        }
        if (this.bindDesc.x < -cc.winSize.width/2){
            this.bindDesc.x = -cc.winSize.width/2;
        }
        if (this.bindDesc.y > (cc.winSize.height/2 - 120)){
            this.bindDesc.y = cc.winSize.height/2 - 120;
        }
        if (this.bindDesc.y < (-cc.winSize.height/2 + 120)){
            this.bindDesc.y = -cc.winSize.height/2 + 120;
        }
        this.lastPos = thisPos;
        // this.updateNewInfo();
        this.node.position = this.bindDesc.position;
    }

    deleteTouch(){
        let clickNode = this.bindDesc;
        this.node.active = false;
        clickNode.destroy();
    }

    editTouch(){
        let clickNode = this.bindDesc;
        if (clickNode.getComponent(cc.Label)){

        }
        else if (clickNode.getComponent('MoteScript')){

        }
        else if (clickNode.getComponent(cc.Sprite)){
            
        }
    }

    rotateAndScaleStart(event){
        this.thisWorldPos = this.bindDesc.convertToWorldSpaceAR(cc.v2(0,0));
        let pos = event.getLocation().sub(this.thisWorldPos);
        let angle =  pos.signAngle(cc.v2(1,0)) *180/ Math.PI ;

        this.startRotateVec = pos;
        this.startRotateAngle = angle;
        this.startScale = this.bindDesc.scale;
        this.startAngle = this.bindDesc.angle;
    }

    rotateAndScaleMove(event){
        let pos = event.getLocation().sub(this.thisWorldPos);
        let angle =  pos.signAngle(cc.v2(1,0)) *180/ Math.PI ;

        this.bindDesc.scale = this.startScale * (pos.mag() / this.startRotateVec.mag());
        this.bindDesc.angle = this.startAngle + this.startRotateAngle - angle;

        this.updateNewInfo();
    }
}
